package com.cgq.controller;

import com.cgq.common.R;
import com.cgq.pojo.User;
import com.cgq.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * *  Author:陈国强
 * *  Description:
 * *  Date:2023/2/13 18:54
 */
@RestController
@RequestMapping("/redis")
@Api(tags = "实现用户的相关接口") //Swagger 接口文档 中文说明
public class RedisController {


    @Autowired
    private UserService service;

    //直接查询数据库   每次查询 都走数据库   每次走走数据库 性能低
    @ApiOperation(value = "普通查询用户信息",notes ="用户列表" )
    @GetMapping("/getUserList/{name}")
//    public List<User> getUserList(){
//        return service.selectUserList();
//    }
    public R selectUsers(@PathVariable String name){
        List<User> users = service.selectUserList(name);
        return R.ok().data("users",users);
    }


    //利用RedisTemplate的缓存进行查询
    @ApiOperation(value = "利用Redis查询用户信息",notes ="用户列表" )
    @GetMapping("/getUserRedisList1/{key}")
//    public List<User> getUserRedisList(){
//        return service.selectUserRedisList();
//    }

    public R getUserRedisList(@PathVariable String key){
        List<User> users = service.selectUserRedisList(key);
        return R.ok().code(200).data("users",users).message("redis查询成功");
    }

    @ApiOperation(value = "利用Redis1查询用户信息",notes ="用户列表" )
    @GetMapping("/getUserRedisList3")
    public R getUserRedisList1(@RequestBody Map<String,Object> params){
        List<User> users = service.selectUserRedisList1(params);
        return R.ok().code(200).data("users",users).message("redis查询成功");
    }

    //利用StringRedisTemplate的缓存进行查询
    @GetMapping("/getUserRedisList2")
    public List<User> getUserRedisList2(){
        return service.selectUserStringRedisList();
    }
}
